/**
 * 用于事件的异步回调简单实现
 */

var eventMap = {

}

exports.emit = function(name, data) {
	if(name in eventMap) {
		console.log('emit: ',name,' value: ',data)
		for(var i = 0; i < eventMap[name].length; i++) {
			eventMap[name][i](data)
		}
	} else {
		console.error('not found ' + name + ' register in')
	}
}

exports.on = function(name, cb) {
	if(name in eventMap) {
		eventMap[name].push(cb)
	} else {
		eventMap[name] = [cb]
	}
}

exports.off = function(name, cb) {
	if(name in eventMap) {
		if(cb == null){
			delete eventMap[name]
		}else {
			for (var i = 0; i < eventMap[name].length; i++) {
				if( eventMap[name][i]===cb){
					eventMap[name].splice(i,1)
				}
			}
		}
	}
}